package com.jilijili.controller;

import com.jilijili.enums.UserInfoModifyType;
import com.jilijili.grace.result.GraceJSONResult;
import com.jilijili.pojo.Users;
import com.jilijili.pojo.bo.UsersUpdateBo;
import com.jilijili.pojo.vo.UsersVo;
import com.jilijili.service.FansService;
import com.jilijili.service.UsersService;
import com.jilijili.utils.redis.RedisOperator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


@Slf4j
@Api(value = "UsersController 用户控制器", tags = "用户接口模块", produces = "ljj", consumes = "app")
@RestController
@RequestMapping("/api/userInfo")
public class UsersController extends BaseInfoProperties {

    @Resource
    private UsersService usersService;

    @Resource
    private FansService fansService;

    @Resource
    private RedisOperator redis;

    // 修改用户
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true),
    })
    @ApiOperation(value = "修改用户")
    @PutMapping("/modifyUserInfo")
    public GraceJSONResult update(@Valid @RequestBody UsersUpdateBo usersUpdateBo,
                                  @RequestParam("type") Integer type) {
        //TODO [1] 检查用户是否符合修改条件
        UserInfoModifyType.checkUserInfoTypeIsRight(type);

        //TODO [2] 修改操作
        Users users = this.usersService.updateUserInfo(usersUpdateBo, type);
        return GraceJSONResult.ok(users);

    }


    // 查询用户
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true),
    })
    @ApiOperation(value = "查询用户")
    @GetMapping("/query")
    public GraceJSONResult query(@RequestParam("userId") String userId,
                                 HttpServletRequest request) {
        Users user = usersService.getUser(userId);

        UsersVo usersVO = new UsersVo();
        BeanUtils.copyProperties(user, usersVO);

        String myFollowsCountsStr = null;
        String myFansCountsStr = null;
        String likedVlogerCountsStr = null;
        // 我的关注博主总数量 redis_my_follows_counts:123123

        if (redis.keyIsExist(REDIS_MY_FOLLOWS_COUNTS + ":" + userId)) {
            String s = this.redis.getStr(REDIS_MY_FOLLOWS_COUNTS + ":" + userId);
            myFollowsCountsStr = s;
        }

        // 我的粉丝总数
        if (redis.keyIsExist(REDIS_MY_FANS_COUNTS + ":" + userId)) {
            myFansCountsStr = this.redis.getStr(REDIS_MY_FANS_COUNTS + ":" + userId);
        }
        // 用户获赞总数，视频博主（点赞/喜欢）总和
        if (redis.keyIsExist(REDIS_VLOGER_BE_LIKED_COUNTS + ":" + userId)) {
            likedVlogerCountsStr = redis.getStr(REDIS_VLOGER_BE_LIKED_COUNTS + ":" + userId);

        }


        Integer myFollowsCounts = 0;
        Integer myFansCounts = 0;
        Integer likedVlogCounts = 0;
        Integer likedVlogerCounts = 0;
        Integer totalLikeMeCounts = 0;

        if (StringUtils.isNotBlank(myFollowsCountsStr)) {
            myFollowsCounts = Integer.valueOf(myFollowsCountsStr);
        }
        if (StringUtils.isNotBlank(myFansCountsStr)) {
            myFansCounts = Integer.valueOf(myFansCountsStr);
        }
//        if (StringUtils.isNotBlank(likedVlogCountsStr)) {
//            likedVlogCounts = Integer.valueOf(likedVlogCountsStr);
//        }
        if (StringUtils.isNotBlank(likedVlogerCountsStr)) {
            likedVlogerCounts = Integer.valueOf(likedVlogerCountsStr);
        }
        totalLikeMeCounts = likedVlogCounts + likedVlogerCounts;

        usersVO.setMyFollowsCounts(myFollowsCounts);
        usersVO.setMyFansCounts(myFansCounts);
        usersVO.setTotalLikeMeCounts(totalLikeMeCounts);


        return GraceJSONResult.ok(usersVO);
    }
}
